package com.ruoyi.yey.picture;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.yey.picture.model.PictureAddModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class PictureService extends ServiceImpl<PictureMapper,PictureEntity> {


    //图片网络路径
//    @Value("${upload.webBasePath}")
    private String webbasepath;


    /**
     * 分页+模糊查询
     * @param page
     * @param picture
     * @return
     */
    public Page<Map> findAllByPage(Page page, PictureEntity picture) {
        return baseMapper.findBySearchVo(page,picture);
    }

    public List<Map> findAllByList(PictureEntity picture) {
        List<Map> pictureList = baseMapper.findBySearchVo(picture);
        return pictureList;
    }

    /**
     * 新增操作
     * @param picture
     */
    @Transactional(rollbackFor = Exception.class)
    public void add(PictureAddModel picture, int index) {
        // 获取学生id
        int studentId = picture.getStudentId();
        //获取图片列表
        for(String pictureUrl: picture.getPictureUrlList()){
            PictureEntity pictureEntity = new PictureEntity();
            if(index==1){
                pictureEntity.setIfTeacher(1);
            }else {
                pictureEntity.setIfTeacher(0);
            }
            pictureEntity.setStudentId(studentId);
            pictureEntity.setPictureUrl(pictureUrl);
            baseMapper.insert(pictureEntity);
        }

    }

    public List<PictureEntity> findByStudentId(int studentId){
        return  baseMapper.selectList(new QueryWrapper<PictureEntity>().eq("student_id",studentId));
    }


    @Transactional(rollbackFor = Exception.class)
    public void edit(PictureEntity picture){
        baseMapper.updateById(picture);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByIds(int[] ids) {
        for(int id :ids){
            baseMapper.deleteById(id);
        }

    }
    /**
     * 新增操作
     * @param billId
     */
    public List<PictureEntity> findChoosePictureList(int billId){
        return  baseMapper.findChoosePictureList(billId);
    }
}
